1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Data.SqlClient;
10 using System.Security.Cryptography;
11 namespace WarehouseManagementSystem
12 {
13 public partial class frmSuppliers : Form
14 {
15
16 SqlConnection con = null;
17 SqlCommand cmd = null;
18 ConnectionString cs = new ConnectionString();
19 public frmSuppliers()
20 {
21 InitializeComponent();
22 }
23 private void Reset()
24 {
25 txtAddress.Text = "";
26 txtCity.Text = "";
27 txtEmail.Text = "";
28 txtSupplierName.Text = "";
29 txtContactNo1.Text = "";
30 txtNotes.Text = "";
31 txtContactNo.Text = "";
32 txtSupplierID.Text = "";
33 btnSave.Enabled = true;
34 btnDelete.Enabled = false;
35 btnUpdate.Enabled = false;
36 txtSupplierName.Focus();
37
38 }
39
40 private void auto()
41 {
42 txtSupplierID.Text = "S-" + GetUniqueKey(6);
43 }
44 public static string GetUniqueKey(int maxSize)
45 {
46 char[] chars = new char[62];
47 chars = "123456789".ToCharArray();
48 byte[] data = new byte[1];
49 RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
50 crypto.GetNonZeroBytes(data);
51 data = new byte[maxSize];
52 crypto.GetNonZeroBytes(data);
53 StringBuilder result = new StringBuilder(maxSize);
54 foreach (byte b in data)
55 {
56 result.Append(chars[b % (chars.Length)]);
57 }
58 return result.ToString();
59 }
60
61
62 private void btnNew_Click(object sender, EventArgs e)
63 {
64 Reset();
65 }
66
67 private void btnSave_Click(object sender, EventArgs e)
68 {
69 if (txtSupplierName.Text == "")
70 {
71 MessageBox.Show("Please enter Supplier name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
72 txtSupplierName.Focus();
73 return;
74 }
75
76 if (txtAddress.Text == "")
77 {
78 MessageBox.Show("Please enter address", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
79 txtAddress.Focus();
80 return;
81 }
82 if (txtCity.Text == "")
83 {
84 MessageBox.Show("Please enter city", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
85 txtCity.Focus();
86 return;
87 }
88
89 if (txtContactNo.Text == "")
90 {
91 MessageBox.Show("Please enter contact no.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
92 txtContactNo.Focus();
93 return;
94 }
95
96 try
97 {
98 auto();
99
100 con = new SqlConnection(cs.DBConn);
101 con.Open();
102
103 string cb = "insert into Supplier(SupplierID,Suppliername,address,City,ContactNo,ContactNo1,Email,Notes) VALUES (@d1,@d2,@d3,@d4,@d5,@d6,@d7,@d8)";
104
105 cmd = new SqlCommand(cb);
106
107 cmd.Connection = con;
108 cmd.Parameters.AddWithValue("@d1", txtSupplierID.Text);
109 cmd.Parameters.AddWithValue("@d2", txtSupplierName.Text);
110 cmd.Parameters.AddWithValue("@d3", txtAddress.Text);
111 cmd.Parameters.AddWithValue("@d4", txtCity.Text);
112 cmd.Parameters.AddWithValue("@d5", txtContactNo.Text);
113 cmd.Parameters.AddWithValue("@d6", txtContactNo1.Text);
114 cmd.Parameters.AddWithValue("@d7", txtEmail.Text);
115 cmd.Parameters.AddWithValue("@d8", txtNotes.Text);
116
117 cmd.ExecuteReader();
118 MessageBox.Show("Successfully saved", "Supplier Details", MessageBoxButtons.OK, MessageBoxIcon.Information);
119 btnSave.Enabled = false;
120 if (con.State == ConnectionState.Open)
121 {
122 con.Close();
123 }
124
125 con.Close();
126
127 }
128 catch (Exception ex)
129 {
130 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
131 }
132 }
133 private void delete_records()
134 {
135
136 try
137 {
138
139 int RowsAffected = 0;
140 con = new SqlConnection(cs.DBConn);
141 con.Open();
142 string cq = "delete from Supplier where SupplierID='" + txtSupplierID.Text + "'";
143 cmd = new SqlCommand(cq);
144 cmd.Connection = con;
145
146 RowsAffected = cmd.ExecuteNonQuery();
147
148 if (RowsAffected > 0)
149 {
150 MessageBox.Show("Successfully deleted", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
151 Reset();
152 }
153 else
154 {
155 MessageBox.Show("No record found", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Information);
156 Reset();
157 }
158 con.Close();
159
160 }
161 catch (Exception ex)
162 {
163 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
164 }
165 }
166
167 private void btnDelete_Click(object sender, EventArgs e)
168 {
169 try
170 {
171
172
173 if (MessageBox.Show("Do you really want to delete the record?", "Supplier Record", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
174 {
175 delete_records();
176 }
177
178 }
179 catch (Exception ex)
180 {
181 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
182 }
183 }
184
185 private void btnUpdate_Click(object sender, EventArgs e)
186 {
187 try
188 {
189 if (txtSupplierName.Text == "")
190 {
191 MessageBox.Show("Please enter Supplier name", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
192 txtSupplierName.Focus();
193 return;
194 }
195
196 if (txtAddress.Text == "")
197 {
198 MessageBox.Show("Please enter address", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
199 txtAddress.Focus();
200 return;
201 }
202 if (txtCity.Text == "")
203 {
204 MessageBox.Show("Please enter city", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
205 txtCity.Focus();
206 return;
207 }
208
209 if (txtContactNo.Text == "")
210 {
211 MessageBox.Show("Please enter contact no.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
212 txtContactNo.Focus();
213 return;
214 }
215 con = new SqlConnection(cs.DBConn);
216 con.Open();
217 string cb = "update Supplier set Suppliername=@d2,address=@d3,City=@d4,ContactNo=@d5,ContactNo1=@d6,Email=@d7,Notes=@d8 where SupplierID=@d1";
218
219 cmd = new SqlCommand(cb);
220
221 cmd.Connection = con;
222 cmd.Parameters.AddWithValue("@d1", txtSupplierID.Text);
223 cmd.Parameters.AddWithValue("@d2", txtSupplierName.Text);
224 cmd.Parameters.AddWithValue("@d3", txtAddress.Text);
225 cmd.Parameters.AddWithValue("@d4", txtCity.Text);
226 cmd.Parameters.AddWithValue("@d5", txtContactNo.Text);
227 cmd.Parameters.AddWithValue("@d6", txtContactNo1.Text);
228 cmd.Parameters.AddWithValue("@d7", txtEmail.Text);
229 cmd.Parameters.AddWithValue("@d8", txtNotes.Text);
230 cmd.ExecuteReader();
231 MessageBox.Show("Successfully updated", "Supplier Details", MessageBoxButtons.OK, MessageBoxIcon.Information);
232 btnUpdate.Enabled = false;
233 if (con.State == ConnectionState.Open)
234 {
235 con.Close();
236 }
237
238 con.Close();
239
240 }
241 catch (Exception ex)
242 {
243 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
244 }
245 }
246
247
248
249
250 private void txtContactNo_KeyPress(object sender, KeyPressEventArgs e)
251 {
252 if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
253 {
254 e.Handled = false;
255 }
256 else
257 {
258 e.Handled = true;
259 }
260 }
261
262 private void txtContactNo1_KeyPress(object sender, KeyPressEventArgs e)
263 {
264 if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
265 {
266 e.Handled = false;
267 }
268 else
269 {
270 e.Handled = true;
271 }
272 }
273
274 private void btnGetData_Click(object sender, EventArgs e)
275 {
276 this.Hide();
277 frmSuppliersRecord1 frm = new frmSuppliersRecord1();
278 frm.Show();
279 frm.GetData();
280 }
281
282 private void frmSuppliers_Load(object sender, EventArgs e)
283 {
284
285 }
286 }
287 }